package org.wmq.taskschedule.algorithm;

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

/**
 * 自适应阈值算法执行器
 * 实现AlgorithmExecutor接口，用于对图像进行自适应阈值处理
 */
public class AdaptiveThresholdExecutor implements AlgorithmExecutor {

    private double maxValue; // 最大值
    private int adaptiveMethod; // 自适应方法
    private int thresholdType; // 阈值类型
    private int blockSize; // 块大小
    private double C; // 常数
    private String description; // 算法描述

    /**
     * 默认构造函数，使用默认参数
     */
    public AdaptiveThresholdExecutor() {
        this.maxValue = 255.0;
        this.adaptiveMethod = Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C;
        this.thresholdType = Imgproc.THRESH_BINARY;
        this.blockSize = 11;
        this.C = 2.0;
        this.description = "自适应阈值算法 - 根据局部区域的亮度自动调整阈值";
    }

    /**
     * 构造函数，允许自定义参数
     * 
     * @param maxValue 最大值
     * @param adaptiveMethod 自适应方法
     * @param thresholdType 阈值类型
     * @param blockSize 块大小
     * @param C 常数
     */
    public AdaptiveThresholdExecutor(double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C) {
        if (blockSize % 2 == 0 || blockSize <= 1) {
            throw new IllegalArgumentException("块大小必须是奇数且大于1");
        }
        this.maxValue = maxValue;
        this.adaptiveMethod = adaptiveMethod;
        this.thresholdType = thresholdType;
        this.blockSize = blockSize;
        this.C = C;
        this.description = "自适应阈值算法 - 根据局部区域的亮度自动调整阈值";
    }

    /**
     * 构造函数，允许自定义所有参数
     * 
     * @param maxValue 最大值
     * @param adaptiveMethod 自适应方法
     * @param thresholdType 阈值类型
     * @param blockSize 块大小
     * @param C 常数
     * @param description 算法描述
     */
    public AdaptiveThresholdExecutor(double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C, String description) {
        if (blockSize % 2 == 0 || blockSize <= 1) {
            throw new IllegalArgumentException("块大小必须是奇数且大于1");
        }
        this.maxValue = maxValue;
        this.adaptiveMethod = adaptiveMethod;
        this.thresholdType = thresholdType;
        this.blockSize = blockSize;
        this.C = C;
        this.description = description;
    }

    @Override
    public Mat execute(Mat input) {
        if (input == null || input.empty()) {
            throw new IllegalArgumentException("输入图像不能为空");
        }

        // 创建输出图像
        Mat output = new Mat();

        // 如果输入不是灰度图像，先转换为灰度
        Mat grayImage = input.clone();
        if (input.channels() > 1) {
            Imgproc.cvtColor(input, grayImage, Imgproc.COLOR_BGR2GRAY);
        }

        // 应用自适应阈值算法
        Imgproc.adaptiveThreshold(grayImage, output, maxValue, adaptiveMethod, thresholdType, blockSize, C);

        // 释放中间资源
        if (!grayImage.equals(input)) {
            grayImage.release();
        }

        return output;
    }

    // Getters and setters
    public double getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(double maxValue) {
        this.maxValue = maxValue;
    }

    public int getAdaptiveMethod() {
        return adaptiveMethod;
    }

    public void setAdaptiveMethod(int adaptiveMethod) {
        this.adaptiveMethod = adaptiveMethod;
    }

    public int getThresholdType() {
        return thresholdType;
    }

    public void setThresholdType(int thresholdType) {
        this.thresholdType = thresholdType;
    }

    public int getBlockSize() {
        return blockSize;
    }

    public void setBlockSize(int blockSize) {
        if (blockSize % 2 == 0 || blockSize <= 1) {
            throw new IllegalArgumentException("块大小必须是奇数且大于1");
        }
        this.blockSize = blockSize;
    }

    public double getC() {
        return C;
    }

    public void setC(double C) {
        this.C = C;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}